<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PnkTreeCategories 
{	
	private $_xmlString = '';
	private $_categoryModel = '';
	private $_postingArea = '';
	
	function createTreeCategoriesForBackend($categoryModel)
	{
	  $this->_categoryModel = $categoryModel;
  	  $this->_xmlString = '<select name=parent_id id=parent_id>' 
  	                    . $this->_tree(0) 
  	                    . "</select>";
      
  	  return $this->_xmlString;
	}
	
  	private function _tree($parent, $level = 0)
  	{    
	    $result = $this->_categoryModel->getAllCategories($parent, 1);
	    
	    foreach ($result as $row) {
    	    $selected = '';
    	    if ($row['category_id'] == $this->_postingArea) {
    	        $selected = 'selected';
    	    }
	      
	        $class = 'category-' .  $level;
	      	$this->_xmlString .= "<option class='{$class}' value='{$row['category_id']}' {$selected} >{$row['name']}</option>\n";
	      	$this->_tree($row['category_id'], $level + 1);
	    }
	    return $this->_xmlString;
  	}
  	
	function createTreeCategories($categoryModel, $postingArea = '')
	{
	  $this->_categoryModel = $categoryModel;
	  $this->_postingArea = $postingArea;
  	  $this->_xmlString = '<select name=posting_area id=posting_area>' 
  	                    . $this->_tree(2) 
  	                    . "</select>";
      
  	  return $this->_xmlString;
	}
}